package com.xiyan.blog.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.xiyan.blog.entity.Blog;
import com.xiyan.blog.entity.BlogTag;
import com.xiyan.blog.service.IBlogService;
import com.xiyan.blog.service.IBlogTagService;
import com.xiyan.blog.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/blog")
@Api(value = "博客", tags = "博客api")
public class BlogController {
    private static final Logger log = LoggerFactory.getLogger(BlogController.class);

    @Autowired
    private IBlogService blogService;

    @Autowired
    private IBlogTagService blogTagService;

    @ApiOperation("新增博客")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult add(@RequestBody Blog blog) {
        try {
            if (ObjectUtil.isNull(blog)) {
                return ResponseResult.error().message("新增的博客为空");
            }

            blogService.saveOrUpdate(blog);

            if (CollUtil.isEmpty(blog.getTags())) {
                return ResponseResult.error().message("无法获得博客标签");
            }

            List<String> tagIds = blog.getTags();
            for (String tagId : tagIds) {
                BlogTag blogTag = new BlogTag();
                blogTag.setBlogId(blog.getId());
                blogTag.setTagId(tagId);
                blogTagService.save(blogTag);
            }
            return ResponseResult.ok();
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseResult.error().message("新增失败");
        }
    }

    @ApiOperation("删除博客")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ResponseResult delete(@PathVariable("id") String id) {
        try {
            if (StrUtil.isEmpty(id)) {
                return ResponseResult.error().message("博客id为空");
            }
            blogService.removeById(id);
            return ResponseResult.ok();
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseResult.error().message("删除失败，博客ID不存在");
        }
    }

    @ApiOperation("博客列表")
    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public ResponseResult list() {
        try {
            List<Blog> blogs = blogService.list();
            return ResponseResult.ok().data(blogs);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseResult.error().message("获取博客列表失败");
        }
    }

    @ApiOperation("获取博客信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseResult getById(@PathVariable("id") String id) {
        try {
            if (StrUtil.isEmpty(id)) {
                return ResponseResult.error().message("博客id为空");
            }

            Blog blog = blogService.getById(id);
            return ResponseResult.ok().data(blog);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseResult.error().message("获取博客信息失败");
        }
    }

    @ApiOperation("获取标签下博客")
    @RequestMapping(value = "/{userId}/{tagId}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseResult getByTagId(@PathVariable("tagId") String tagId,
                                     @PathVariable("userId") String userId) {
        try {
            if (StrUtil.isEmpty(tagId)) {
                return ResponseResult.error().message("标签为空");
            }

            if (StrUtil.isEmpty(userId)) {
                return ResponseResult.error().message("用户ID为空");
            }

            List<Blog> blogs = blogService.findByTagId(userId, tagId);
            return ResponseResult.ok().data(blogs);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseResult.error().message("获取标签下博客失败");
        }
    }

    // TODO: 多条件分页查询
}
